//
//  SHD_ExectWebViewController.m
//  B段项目
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "SHD_ExectWebViewController.h"

@interface SHD_ExectWebViewController ()<UIWebViewDelegate>
@property(nonatomic, strong)NSString *night;
@property(nonatomic, strong)XMN_ProgressHUD *HUD;
@end

@implementation SHD_ExectWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataWeb = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 375 * FitWidth, (667 - 64) * FitHeight)];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(310 * FitWidth, 30 * FitHeight, 50 * FitWidth, 10 * FitHeight);
    button.titleLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:8 * FitWidth];
    [button setTitleColor:[UIColor colorWithHue:0.42 saturation:0.84 brightness:0.98 alpha:1] forState:UIControlStateNormal];
    [button setTitle:@"More·小组" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    _titleLabel = [[myTitleLabel alloc]initWithFrame:CGRectMake(20 * FitWidth, button.frame.size.height + button.frame.origin.y + 20 * FitHeight, 200 * FitWidth, 30 * FitHeight)];
    UIImageView *userImageView =[[UIImageView alloc]initWithFrame:CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.origin.y + _titleLabel.frame.size.height + 20 * FitHeight, 40 * FitWidth, 40 * FitHeight)];
    userImageView.layer.cornerRadius = 20 * FitHeight;
    userImageView.backgroundColor = [UIColor blackColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(userImageView.frame.origin.x + userImageView.frame.size.width + 10 * FitWidth, _titleLabel.frame.origin.y + _titleLabel.frame.size.height + 40 * FitHeight, 50 * FitWidth, 10 * FitHeight)];
    label.text = @"楼主";
    label.font = [UIFont fontWithName:@"STHeitiSC-Light" size:10 * FitWidth];
    _ID = _Model.contentid;

    _titleLabel.text = _Model.title;
    

    _topView = [[myView alloc]initWithFrame:CGRectMake(0, 0, 375 *FitWidth, 200 * FitHeight)];
    
                      
    _dataWeb.scrollView.subviews.firstObject.frame = CGRectMake(0, 200 * FitHeight, 375 *FitWidth, (667 - 64 - 200) * FitHeight);
    _dataWeb.delegate = self;
    [_dataWeb NightWithType:UIViewColorType2];
    [_topView addSubview:button];
    [_topView addSubview:label];
    [_topView addSubview:userImageView];
    [_topView addSubview:_titleLabel];
    [_dataWeb.scrollView addSubview:_topView];
    [self getData];
    [self.view addSubview:_dataWeb];
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"nightModel"]) {
        _night = @"1";
    }else{
        _night = @"0";
    }
    
    //注册消息中心
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeNight:) name:@"WebNight" object:nil];
}
-(void)buttonAction
{
    
}
-(void)changeNight:(NSNotification *)notification{
    _night = (NSString *)[notification object];
}
-(void)getData
{
    
    [HTTPMethod getDataByString:@"http://api2.pianke.me/group/posts_info" BODYDic:[ChangeString changeStringToDictionary:[NSString stringWithFormat:@"auth=&client=1&contentid=%@&deviceid=5AA12B08-7AD2-4ED0-B4EB-571025443952&version=3.0.6", _ID]] WithDataBlock:^(id data) {
        NSString *string = [[[data objectForKey:@"data"] objectForKey:@"postsinfo"] objectForKey:@"html"];
        [_dataWeb loadHTMLString:string baseURL:nil];
    }];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    if ([_night isEqualToString:@"1"]) {
        [_dataWeb stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.backgroundColor= '#555555'"];
        //字体颜色
        [_dataWeb stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
    }
    
    //------------------------------------------------------------
    //修改JS语言
    //定义JS字符串(JavaScript)
    NSString *script = [NSString stringWithFormat:@"var script = document.createElement('script');"
                        "script.type = 'text/javascript';"
                        "script.text = \"function ResizeImages() { "
                        "var myimg;"
                        "var maxwidth = %f;"//屏幕宽度
                        "for (i = 0; i < document.images.length; i++) {"
                        "myimg = document.images[i];"
                        "myimg.height = maxwidth / (myimg.width / myimg.height);"
                        "myimg.width = maxwidth;"
                        "}"
                        "}\";"
                        "document.getElementsByTagName('p')[0].appendChild(script);", 375 * FitWidth - 15 * FitWidth];
    //添加JS
    [webView stringByEvaluatingJavaScriptFromString:script];
    //添加调用JS执行的语句
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    //------------------------------------------------------------
    
    [_HUD removeFromSuperview];
}

-(void)viewWillAppear:(BOOL)animated{
    _HUD = [[XMN_ProgressHUD alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, Height)];
    _HUD.backgroundColor = [UIColor colorWithRed:0.89 green:0.89 blue:0.9 alpha:1];
    _HUD.XMN_label.text = @"Waiting...";
    [self.view addSubview:_HUD];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
